<template>
	<div class="invite-wrap">
    <div class="invite-wrap-img"><img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin_bg.png" alt=""></div>
    <div class="invite-top">
      <div class="invite-top-item">
        <div class="invite-top-item-left">
          <div class="img">
            <img :src="goodInfo.pic_url" alt="" mode="scaleToFill">
          </div>
        </div>
        <div class="invite-top-item-right">
          <div class="title">{{goodInfo.title}}</div>
          <div class="info">
            <span class="icon"><i>活动价</i></span>
            <div class="new-price">
              <span class="price" v-html="scaleGoodsPriceFn(goodInfo.pay_price, 'nowprice')"></span>
            </div>
            <div class="origin-price">
              <del>&yen;{{goodInfo.original_price}}</del>
            </div>
          </div>
        </div>
      </div>
      <div class="invite-top-item-process">
        <p class="money">
          <span>已砍<span class="k-num">{{goodInfo.bargin_amount}}</span>元，还剩<span class="s-num">{{parseFloat(goodInfo.pay_price-goodInfo.bargin_amount).toFixed(2)}}</span>元</span>
        </p>
        <p class="time">
          <span>还剩</span>
          <!-- <span>
            <van-count-down :time="time" @change="timeChage">
              <template v-slot="timeData">
                <span class="">{{ timeData.days }}</span>
                <span class="colon">天</span>
                <span class="time-bg">{{ timeData.hours }}</span>
                <span class="colon">:</span>
                <span class="time-bg">{{ timeData.minutes }}</span>
                <span class="colon">:</span>
                <span class="time-bg">{{ timeData.seconds }}</span>
              </template>
            </van-count-down>
          </span> -->
          <span v-if="countDownList" class="time-count" v-html="countDownList"></span>
          <span v-else>
            <span class="">00</span>
            <span class="colon">天</span>
            <span class="time-bg">00</span>
            <span class="colon">:</span>
            <span class="time-bg">00</span>
            <span class="colon">:</span>
            <span class="time-bg">00</span>
          </span>
          <span>砍价结束</span>
        </p>
      </div>
      <div class="invite-btn">
        <!-- TODO 小程序分享处理 -->
        <wx-button class="btn red-gra-btn" open-type="share">马上邀请好友砍价</wx-button>
      </div>
      <div class="invite-top-step">
        <div class="invite-top-step-img">
          <img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin.png" alt="">
        </div>
      </div>
      <div class="invite-bottom mgt20">
        <div class="invite-bottom-title">
          <span class="img"><img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin_info.png" alt=""></span>
          <span class="tit">砍价说明</span>
          <span class="img"><img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin_info.png" alt=""></span>
        </div>
        <div class="invite-bottom-wrap">
          <div class="invite-bottom-wrap-item">
            <div class="icon">1</div>
            <div class="text">点击邀请好友砍价</div>
          </div>
          <div class="invite-bottom-wrap-item">
            <div class="icon">2</div>
            <div class="text">马上参与好友砍价活动，点击立即砍价帮助好友砍价.</div>
          </div>
          <div class="invite-bottom-wrap-item">
            <div class="icon">3</div>
            <div class="text">同时也可以立即购买对应的商品或者邀请其它好友，并且你也可以点击“我也要砍”，让好友帮你砍价.</div>
          </div>
        </div>
      </div>
    </div>
    
    <van-overlay :show="sharePopShow">
      <div class="wrapper" @click="sharePopShow = false">
        <img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin_share.png" alt="">
      </div>
    </van-overlay>

    <!-- 授权弹出框 -->
    <small-login ref="smalllogin"></small-login>
  </div>
</template>

<script>
  /**
   * @Author: yqe@360shop.cn
   * @Date: 2019-11-29 09:38:45
 * @Last Modified by: zhangmeng
 * @Last Modified time: 2020-08-17 13:50:56
   */
  import Vue from 'vue'
  import { CountDown } from 'vant'
  import { Overlay } from 'vant'
  Vue.use(CountDown)
  Vue.use(Overlay)
  import { mpShare } from '@/utils/utils'
  import small from '@/smallapp/small'
  import { scaleGoodsPrice } from '@/utils/index.js'
  import { getBarginIndex } from '@/api/item/activity.js'
  import SmallLogin from '@/components/SmallLogin/smallLogin'
  export default Vue.extend({
    name: 'index',
    components: {
      SmallLogin
    },
    data() {
      return {
        time: 0,
        sharePopShow: false,
        barginId: 0,
        shareId: 0,
        goodInfo: {},
        itemId: 0,
        timeData: {
          days: '00',
          hours: '00',
          minutes: '00',
          seconds: '00'
        },
        countDownList: '',
        interval: null,
        shareParam:{}
      }
    },
    created() {
      this.init()
    },
    mounted() {
        window.addEventListener('wxshow', () => {
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
          }
        })
        window.$$subscribe('loginReload', reload => {
          if (reload) {
            this.init()
          }
        })
    },
    methods: {  
      init() {
        // console.log(2222)
        // console.log(this.$route.query)
        const sendData = {}
        sendData.id = this.$route.query.id
        sendData.share_id = this.$route.query.pid
        this.getBarginIndexFun(sendData)
      },
      /**
       * 商品价格 整数放大
       */
      scaleGoodsPriceFn(price, className) {
        return scaleGoodsPrice(price, className)
      },

      timeChage(timeData) {
        this.timeData = {
          days: this.timeFormat(timeData.days),
          hours: this.timeFormat(timeData.hours),
          minutes: this.timeFormat(timeData.minutes),
          seconds: this.timeFormat(timeData.seconds)
        }
      },

      /**
       * 分享弹出层
       */
      shareFriends(show) {
        this.sharePopShow = show
      },

      /**
       * 立即购买
       */
      nowBuyItem(id) {
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
            return false
          }
        this.skuDialogVisible = true
        this.itemId = id
      },

      /**
       * 我也要砍价
       */
      navToDetail(id) {
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
            return false
          }
        this.$JumpName(this, 'item-detail', { 'detailId': id })
      },

      /**
       * 获取砍价基本数据
       */
      getBarginIndexFun(sendData) {
        getBarginIndex(sendData).then(res => {
          if (res.status === 1) {
            this.shareId = this.$route.query.pid
            this.itemId = res.data.info.item_id
            this.barginId = res.data.info.item_bargin_id
            this.goodInfo = res.data.info
            const endTime = res.data.info.end_time * 1000
            this.setIntervalTime(endTime)
            this.shareParam= {'id':res.data.info.item_id,'bargin_id':res.data.info.item_bargin_id,'uid':6765}

              // TODO 小程序分享
              const shareInfo = {
                title: res.data.jsapi_title,
                imgUrl: res.data.jsapi_img,
                shareParam: res.data.jsapi_url.split('?')[1]
              }
              mpShare (true, false, '/goods/pages/itemBargainDetail/index', shareInfo,'/item-bargain-detail')
          } else {
            this.$Error(res.msg)
            // 返回上一页，与测试确认过
            setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
            }, 1000)
          }
        })
      },

      // 分享成功的回调
      shareSuccess() {
        // 用户确认后执行的回调函数
        // console.log('分享成功')
        this.$JumpName(this, 'item-bargain-detail', { 'id': this.itemId, 'uid': this.shareId })
      },

      /**
       * 补全小于10的
       */
      timeFormat(param) {
        return param < 10 ? '0' + param : param
      },

      // 设置定时器做倒计时
      setIntervalTime(end) {
        this.interval = setInterval(() => {
          this.countDownList = this.getResidueTime(end)
        }, 1000)
      },
      // 获取剩余时间
      getResidueTime(end) {
        // console.log(end)
        const nowtime = new Date().getTime() // 当前时间 毫秒数
        // const endTime = Date.parse(new Date(end.replace(/-/g, '/'))) // 结束时间  毫秒数
        const endTime = end
        const totalSeconds = (endTime - nowtime) / 1000 // 结束时间-当前时间 = 剩余多少时间
        let day = 0
        let hour = 0
        let minute = 0
        let second = 0
        if (totalSeconds > 0) {
          day = parseInt(totalSeconds / 3600 / 24) // 天
          hour = parseInt((totalSeconds / 3600) % 24) // 时
          minute = parseInt((totalSeconds / 60) % 60) // 分
          second = parseInt(totalSeconds % 60) // 秒
        } else {
          day = 0 // 天
          hour = 0 // 时
          minute = 0 // 分
          second = 0 // 秒
          clearInterval(this.interval)
        }
        hour = this.addZero(hour)
        minute = this.addZero(minute)
        second = this.addZero(second)
        const residueTime = `<span class="num day">${day}</span><span class="pot day">天</span><span class="time-bg">${hour}</span><span class="pot">:</span><span class="time-bg">${minute}</span><span class="pot">:</span><span class="time-bg">${second}</span>`

        return residueTime
      },
      // 补齐格式不满10加0
      addZero(n) {
        return n < 10 ? '0' + n : n
      }
    },
    beforeDestroy() { // 组件的销毁
      clearInterval(this.interval) // 清除定时器
      this.interval = null
      window.$$unsubscribe('loginReload')
    }
  })
</script>
<style lang="scss">
  .time {
    .time-bg {
      background-color: #1A1A1A;
      color: #fff;
      width: 42px;
      height: 38px;
      line-height: 38px;
      text-align: center;
      display: inline-block;
      border-radius: 4px;
    }
    .pot {
      margin: 0 10px;
    }
  }

  .van-count-down {
    display: inline-block;
  }
</style>
<style lang="scss">
  @import "src/styles/mixin";
  @import "src/styles/variables";
  .invite-wrap{
    // padding: 20px;
    position: relative;
    &-img {
      width: 100%;
      height: 332px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .invite-top{
      // position: absolute;
      // top: 30px;
      // left: 25px;
      // right: 25px;
      position:relative;
      background: #fff;
      border-radius: 20px;
      overflow: hidden;
      padding: 25px;
      margin:-300px 25px 0;
      &-item{
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-bottom: 20px;
        &-left{
          margin-right: 20px;
          .img{
            width: 150px;
            height: 150px;
            overflow: hidden;
            img{
              width: 100%;
              height: 100%;
            }
          }
        }
        &-right{
          width: 70%;
          flex: 1;
          height: 150px;
          position: relative;
          padding: 10px 0;
          box-sizing: border-box;
          .title{
            // font-size: 24px;
            color: #333;
            font-weight: bold;
            line-height: 38px;
            vertical-align: top;
            @include lineClamp(24px, 1.4, 2);
          }
          .info{
            position: absolute;
            bottom: 10px;
            .icon {
              display: inline-block;
              color: #fff !important;
              transform: skewX(-10deg);
              background-color: #FE1B49;
              width: 96px;
              height: 36px;
              line-height: 36px;
              text-align: center;
              border-radius: 4px;
              margin-right: 10px;
              i{
                display:inline-block;
                transform: skewX(10deg);
              }
            }
            .new-price{
              font-size: 0;
              display: inline-block;
            }
            .origin-price{
              display: inline-block;
              font-size: 24px;
              margin-left: 20px;
              color: #999;
            }
          }
        }
        &-process {
          width: 100%;
          text-align: center;
          padding: 20px 0 70px;
          font-size: 26px;
          color: #1A1A1A;
          .money {
            margin-bottom: 40px;
            .k-num {
              font-size: 28px;
              color: #F21228;
              font-weight: bold;
              margin: 0 5px;
            }
            .s-num {
              color: #000;
              font-weight: bold;
              font-size: 28px;
              margin: 0 5px;
            }
          }
          .time {
            .time-bg {
              background-color: #1A1A1A;
              color: #fff;
              width: 42px;
              height: 38px;
              line-height: 38px;
              text-align: center;
              display: inline-block;
              border-radius: 4px;
            }
          }
        }
      }
      &-step{
        margin-top: 15px;
        &-img{
          width: 610px;
          margin: 0 auto;
          padding: 40px 0;
          img{
            width: 100%;
          }
        }
      }
    }
    .invite-bottom{
      background:rgba(255,242,243,1);
      border-radius:20px;
      padding: 20px 0;
      &-title{
        text-align: center;
        padding: 20px 0;
        .tit{
          font-size: 32px;
          color: #F21228;
          font-weight: bold;
        }
        .img{
          @include alignTop;
          width: 18px;
          height: 22px;
          margin: 0 5px;
          img{
            vertical-align: -6px;
            width: 100%;
          }
        }
      }
      &-wrap{
        &-item{
          display: flex;
          flex-direction: row;
          padding: 20px;
          .icon{
            width: 40px;
            height: 40px;
            line-height: 38px;
            font-size: 26px;
            text-align: center;
            color: #fff;
            background: #F21228;
            margin-right: 15px;
            border-top-left-radius: 50%;
            border-top-right-radius: 50%;
            border-bottom-right-radius: 50%;
            font-weight: bold;
          }
          .text{
            flex: 1;
            font-size: 28px;
            line-height: 36px;
            color: #666;
            margin-top: 6px;
          }
        }
        &-kjb-item {
          display: flex;
          flex-direction: row;
          flex-wrap: nowrap;
          justify-content: space-between;
          border-top: 1px dashed #ff7979;
          margin: 0 25px;
          padding: 30px 0px;
          font-size: 26px;
          font-weight: bold;
          &:first-child {
            margin-top: 20px;
          }
          .user {
            &>span {
              &:first-child {
                color: #F21228;
                font-size: 32px;
                font-weight: bold;
              }
            }
            .user-img {
              width: 66px;
              height: 66px;
              display: inline-block;
              border-radius: 50%;
              vertical-align: -20px;
              margin: 0 5px;
              &>img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
              }
            }
          }
          .money {
            line-height: 66px;
            &>span {
              &:last-child {
                color: #F21228;
              }
            }
          }
        }
      }
    }
    .invite-tip {
      width: 100%;
      text-align: center;
      color: #F21228;
      font-size: 32px;
      padding-bottom: 30px;
    }
    .invite-btn{
      .btn{
        font-size: 36px;
        line-height: 36px;
        padding: 24px 0;
        text-align: center;
        color: #fff;
        border-radius: 60px;
        margin-bottom: 30px;
        &:last-child {
          margin-bottom: 0;
        }
      }
    }
    .wrapper {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
</style>
